#include "userinfowidget.h"
#include "mainwidget.h"
#include <QMessageBox>
/**********************************************************************************************
 * 用户的个人信息
 **********************************************************************************************/
UserInfoWidget::UserInfoWidget(QWidget *parent, const UserInfo &userInfo)
    :QDialog(parent)
    ,userInfo(userInfo)
{
    //1.设置Dialog基本布局
    this->setFixedSize(400,200);
    this->setWindowTitle("用户详情");
    this->setWindowIcon(QIcon(":/resource/selfInfo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->move(QCursor::pos());

    //2.创建布局管理器
    QGridLayout* glayout = new QGridLayout();
    glayout->setVerticalSpacing(10);
    glayout->setHorizontalSpacing(20);
    glayout->setContentsMargins(40, 20, 0, 0);
    glayout->setAlignment(Qt::AlignTop);
    this->setLayout(glayout);

    // 3. 添加头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75, 75);
    avatarBtn->setIconSize(QSize(75, 75));
    avatarBtn->setStyleSheet("QPushButton {border:none};");
    avatarBtn->setIcon(userInfo.avatar);

    QString labelStyle = "QLabel { font-weight: 800; padding-left: 20px;}";
    QString btnStyle = "QPushButton { border: 1px solid rgb(100, 100, 100); border-radius: 5px; background-color: rgb(240, 240, 240); }";
    btnStyle += "QPushButton:pressed { background-color: rgb(205, 205, 205); }";

    int width = 80;
    int height = 30;

    // 4. 添加用户序号
    idTag = new QLabel();
    idTag->setText("序号");
    idTag->setStyleSheet(labelStyle);
    idTag->setFixedSize(width, height);
    idTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    idLabel = new QLabel();
    idLabel->setText(userInfo.userId);
    idLabel->setFixedSize(width, height);
    idLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);


    // 5. 添加用户昵称
    nameTag = new QLabel();
    nameTag->setText("昵称");
    nameTag->setStyleSheet(labelStyle);
    nameTag->setFixedSize(width, height);
    nameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    nameLabel = new QLabel();
    nameLabel->setText(userInfo.nickName);
    nameLabel->setFixedSize(width, height);
    nameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);


    // 6. 设置电话
    phoneTag = new QLabel();
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(labelStyle);
    phoneTag->setFixedSize(width, height);
    phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    phoneLabel = new QLabel();
    phoneLabel->setText(userInfo.phoneNumber);
    phoneLabel->setFixedSize(width, height);
    phoneLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 7. 添加功能按钮
    applyBtn = new QPushButton();
    applyBtn->setText("申请好友");
    applyBtn->setFixedSize(80, 30);
    applyBtn->setStyleSheet(btnStyle);
    applyBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    sendMessageBtn = new QPushButton();
    sendMessageBtn->setText("发送消息");
    sendMessageBtn->setFixedSize(80, 30);
    sendMessageBtn->setStyleSheet(btnStyle);
    sendMessageBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setFixedSize(80, 30);
    deleteFriendBtn->setStyleSheet(btnStyle);
    deleteFriendBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    //8.将上述控件添加到布局管理器中
    glayout->addWidget(avatarBtn,0,0,3,1);

    glayout->addWidget(idTag,0,1);
    glayout->addWidget(idLabel,0,2);

    glayout->addWidget(nameTag, 1, 1);
    glayout->addWidget(nameLabel, 1, 2);

    glayout->addWidget(phoneTag, 2, 1);
    glayout->addWidget(phoneLabel, 2, 2);

    glayout->addWidget(applyBtn, 3, 0);
    glayout->addWidget(sendMessageBtn, 3, 1);
    glayout->addWidget(deleteFriendBtn, 3, 2);

    //9.初始化按钮的禁用关系
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    auto* myFriend = dataCenter->findFriendById(this->userInfo.userId);
    if(myFriend == nullptr)
    {
        //不是好友
        sendMessageBtn->setEnabled(false);
        deleteFriendBtn->setEnabled(false);
    }
    else
    {
        //是好友
        applyBtn->setEnabled(false);
    }
    //10.初始化信号槽
    initSingalSlot();
}

void UserInfoWidget::initSingalSlot()
{
    //1.发送消息
    connect(sendMessageBtn,&QPushButton::clicked,this,[=](){
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchSession(userInfo.userId);
        this->close();  //将本窗口（用户信息展示）关闭
    });

    //2.删除好友
    connect(deleteFriendBtn,&QPushButton::clicked,this,&UserInfoWidget::clickDeleteFriendBtn);

    //3. 添加好友
    connect(applyBtn,&QPushButton::clicked,this,&UserInfoWidget::clickApplyFriendBtn);
}

void UserInfoWidget::clickDeleteFriendBtn()
{
    //1.弹出确认对话框
    auto result = QMessageBox::warning(this,"确认删除","确认删除该好友?",QMessageBox::Ok | QMessageBox::Cancel);
    if(result != QMessageBox::Ok)
    {
        LOG() << "取消删除好友";
        return;
    }
    //2.发送网络请求
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->deleteFriendAsync(userInfo.userId);
    this->close();
}

void UserInfoWidget::clickApplyFriendBtn()
{
    //1.发送添加好友的请求
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->applyFriendAsync(userInfo.userId);
    //2.关闭当前窗口
    this->close();
}
